﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceInvaders
{
    public static class Ranking
    {
        public static string[] SCORES = new string[10];
        public static string[] NAMES = new string[10];
        static Vector2 FIRSTSCOREPOSITION = new Vector2(350, 50);
        public static SpriteFont FONT;
        public static Texture2D BACKGROUND;
        public static Icon VOLTAR;
        public static bool writeScore;
        static string name;

        public static void Update(MouseState mState)
        {
            if (writeScore)
            {
                TecladoVirtual.Update(mState, ref name);
            }
            else
                if (mState.LeftButton == ButtonState.Pressed)
                {
                    if (VOLTAR.CheckMouse(mState))
                    {
                        GerenciadorDeCena.faseAtual = EnumCenas.MENU;
                    }
                }
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(BACKGROUND, new Vector2(0, 0), Color.White);
            spriteBatch.Draw(VOLTAR.texture, VOLTAR.position, Color.White);
            for (int n = 0; n < NAMES.Length; n++)
            {
                spriteBatch.DrawString(FONT, NAMES[n] + ":\n" + SCORES[n],new Vector2(FIRSTSCOREPOSITION.X, FIRSTSCOREPOSITION.Y * n), Color.White);
            }

            if (writeScore)
            {
                TecladoVirtual.Draw(spriteBatch);
                if (name != null)
                {
                    spriteBatch.DrawString(Game1.testFont, name, new Vector2(50, 50), Color.GreenYellow);
                }
            }
        }

        public static void WriteScore(string name)
        {
            StreamWriter write = new StreamWriter(@"..\..\..\..\p.spc", true);
            write.WriteLine(name);
            write.WriteLine(Ponto.Points);
            write.Close();
            name = null;
            writeScore = false;
        }

        public static void LoadScores()
        {
            ushort count = 1;
            ushort s = 0;
            ushort n = 0; 
            
            using (StreamReader read = new StreamReader(@"..\..\..\..\p.spc"))
            {
                string line;

                while ((line = read.ReadLine()) != null)
                {
                    if (n >= 10 || s >= 10)
                    {
                        break;                        
                    }
                    if (count % 2 != 0)
                    {
                        NAMES[n] = line;
                        n++;
                    }
                    else
                    {
                        SCORES[s] = line;
                        s++;
                    }
                    count++;                    
                }                
            }
            while (n <= 9)
            {
                NAMES[n] = "------";
                SCORES[s] = "------";
                n++;
                s++;
            }
        }
    }
}
